features := ''
name := `basename "$(pwd)"`
target := `drone print target 2>/dev/null || echo ""`
release_bin := "target/" + target + "/release/" + name

# Install dependencies
deps:
	type cargo-objdump >/dev/null || cargo +stable install cargo-binutils
	type drone >/dev/null || cargo install drone
	rustup target add $(drone print target)

# Reformat the source code
fmt:
	cargo fmt

# Check the source code for mistakes
lint:
	cargo clippy --features "\{{features}}"

# Build the binary
build:
	cargo build --features "\{{features}}" --release

# Build the documentation
doc:
	cargo doc --features "\{{features}}"

# Open the documentation in a browser
doc-open: doc
	cargo doc --features "\{{features}}" --open

# Run the tests
test:
	cargo test --features "std \{{features}}" \
		--target=$(rustc --version --verbose | sed -n '/host/{s/.*: //;p}')

# Display information from the binary
dump: build
	cargo objdump --features "\{{features}}" --release --bin \{{name}} -- \
		--disassemble --demangle --full-contents --all-headers --syms \
		| pager

# Display the sizes of sections inside the binary
size +args='': build
	cargo size --features "\{{features}}" --release --bin \{{name}} -- \{{args}}

# Display the result of macro expansion
expand:
	cargo rustc --features "\{{features}}" --lib -- -Z unstable-options --pretty=expanded

# Assert the reset signal
reset:
	drone reset

# Write the binary to ROM
flash: build
	drone flash \{{release_bin}}

# Run a GDB session
gdb:
	drone gdb \{{release_bin}} --reset

# Run a GDB session as a backend for a debugger GUI or an IDE
@gdb-mi:
	drone gdb \{{release_bin}} --reset -i=mi -- -nx

# Capture the log output
log:
	drone log --reset :0:1

# Record `heaptrace` file (`trace_port` option in `heap!` macro should be enabled)
heaptrace:
	truncate -s0 heaptrace
	drone log --reset :0:1 heaptrace:31
